<script setup lang='ts'>
  import { nextTick, computed, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '130px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    list:[] as Array<any>,
    gameList:[] as Array<any>
  })
  interface formQuert {
    id:number
    game_id:number | string
    content:string
    circulate:number | string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    content: [{ required: true, trigger: 'blur', message: '请输入描述' }],
    circulate: [{ required: true, trigger: 'blur', message: '请输入循环次数' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      const { id=0 } = form
      let submitSet = false
      if(id) {
        await equipApi.whiteEdit(form).then(res=>{
          submitSet = true
          ToastSuccess('新增白名单成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await equipApi.whiteAdd(form).then(res=>{
          submitSet = true
          ToastSuccess('编辑白名单成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }

    })
  }
  const openDialog = async(query:any = {})=> {
    const {
      id='', content='', circulate='', gameList=[]
    } = query
    action.title = `${id?'编辑':'新增'}ID:${id}二级礼物池`
    action.gameList = gameList
    const obj:any = {
      circulate, content
    }
    if(id) {
      obj['id'] = id
    }else {
      obj['game_id'] = ''
    }
    Object.assign(form,obj)
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="55%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item v-if="!form.id" :label-width="formLabelWidth" label="游戏类型">
          <el-select v-model="form.game_id" clearable placeholder="请选择游戏类型">
            <el-option v-for="item in action.gameList" :label="item.game_name" :value="item.game_id" />
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="循环次数" prop="circulate">
          <el-input v-model="form.circulate" @input="allNumber(form,'circulate')" placeholder="请输入循环次数" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="白名单描述" prop="content">
          <el-input
            :disabled="action.disabled"
            v-model="form.content"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请输入白名单描述"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
